package com.bjpowernode.app.services;

import com.bjpowernode.app.beans.ClassRoom;
import com.bjpowernode.app.dao.ClassRoomDao;
import com.bjpowernode.app.dao.ClassRoomDaoImp;
import com.bjpowernode.app.exception.MyException;

import java.util.ArrayList;

public class ClassRoomServicesImp implements ClassRoomServices{


    ClassRoomDao classRoomDao=new ClassRoomDaoImp();


    @Override
    public ArrayList<ClassRoom> getAll() {
        return classRoomDao.getAll();
    }

    @Override
    public ClassRoom get(int id) {
        return classRoomDao.get(id);
    }

    @Override
    public void add(ClassRoom classRoom) throws MyException {


//        ClassRoom classRoomTemp = classRoomDao.get(classRoom.getId());
//
//        if(classRoomTemp!=null){
//            throw  new MyException("班级名称ID重复");
//        }

        if(classRoom.getName()==null || classRoom.getName().length()>10 || classRoom.getName().length()==0){
            throw  new MyException("班级名称长度必须是1-10");
        }

        //负责处理班级ID
        ArrayList<ClassRoom> all = getAll();
        //获取集合最后个班级的id并且+1
        //极端情况为集合为空
        if(all.size()==0){
            classRoom.setId(1001);
        }
        else {
            classRoom.setId(all.get(all.size()-1).getId()+1);
        }

        classRoomDao.add(classRoom);

    }

    @Override
    public void edit(ClassRoom classRoom) throws MyException {

        if(classRoom.getName().length()>10 || classRoom.getName().length()==0){
            throw  new MyException("班级名称长度必须是1-10");
        }

        classRoomDao.edit(classRoom);

    }

    @Override
    public void del(int id) throws MyException {
        classRoomDao.del(id);
    }
}
